package cn.zyjblogs.starter.feign.hystrix;

import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

/**
 * @author zhuyijun
 */
public class BaseContextHystrixConcurrencyStraategy extends HystrixConcurrencyStrategy {
    private static final Logger log = LoggerFactory.getLogger(BaseContextHystrixConcurrencyStraategy.class);
    private final HystrixConcurrencyStrategy hystrixConcurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();

    public BaseContextHystrixConcurrencyStraategy() {
        HystrixPlugins.reset();
        HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
    }

    @Override
    public <T> Callable<T> wrapCallable(Callable<T> callable) {
        log.debug("触发熔断策略");
        return this.hystrixConcurrencyStrategy != null ?
                this.hystrixConcurrencyStrategy.wrapCallable(new BaseContextHystrixCallable<>(callable))
                : super.wrapCallable(new BaseContextHystrixCallable<>(callable));
    }
}
